package com.itboye.ihomebank.web;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.activity.qianyue.PayActivity;
import com.itboye.ihomebank.util.ByAlert;
import com.tencent.tauth.Tencent;

import java.util.Observable;
import java.util.Observer;

public class WebActivity extends BaseOtherActivity implements Observer {
    View v_statusbar;
    WebView webview;
    TextView add_shap_title_tv;
    private String urls;
    private String title;
    ImageView close_icon, img_other;
    ProgressBar myProgressBar;
    WebSettings settings;
    StringBuilder builder = new StringBuilder();
    String pid;
    String groupid;
    String urlString = "com.moodwo.mengwu://?type=MENGWU_PRODUCT_DETAIL&pid=";
    private String APP_ID = "wxae95316b3a4998a6";
    private String QQAPP_ID = "1105450171";
    Tencent mTencent;


    BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // Intent intent2 = new Intent("set");
            // intent2.putExtra("index", 3);
            // sendBroadcast(intent2);
            finish();
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack(); // goBack()表示返回WebView的上一页面
            return true;
        } else {
            finish();
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.img_other:

                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    @Override
    protected int layout_id() {
        // TODO Auto-generated method stub
        return R.layout.activity_web;
    }

    @Override
    protected void onInitController() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onInit() {
        // TODO Auto-generated method stub
//        img_other.setVisibility(View.VISIBLE);
//        img_other.setBackgroundResource(R.drawable.jiantou);
//		img_other.setVisibility(View.GONE);
        setStatusBarHeight(v_statusbar);
        title = getIntent().getStringExtra("title");
        add_shap_title_tv.setText(title);
        urls = getIntent().getStringExtra("url");
        IntentFilter filter = new IntentFilter("finish");
        registerReceiver(receiver, filter);
        Log.e("hu=======", "url==" + urls);
        //支持javascript   表示不支持js，如果想让java和js交互或者本身希望js完成一定的功能请把false改为true。
        webview.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        webview.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        webview.getSettings().setBuiltInZoomControls(true);
        //扩大比例的缩放(设置WebView支持viewpoart)
        webview.getSettings().setUseWideViewPort(true);
        //自适应屏幕(设置网页超过屏幕宽度时重新布局为屏幕宽度)
        webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webview.getSettings().setLoadWithOverviewMode(true);

        webview.loadUrl(urls);

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub

                builder = new StringBuilder();
                builder.append(url);
                Log.e("hu====", "url==" + url);
                ByAlert.alert(url+"");
                if (url.contains("QIANMI_PAY")) {
                    Intent intent = new Intent(WebActivity.this,
                            PayActivity.class);
                    String itemid = builder.substring(builder.indexOf("items=") + 6,
                            builder.lastIndexOf("&"));
                    String money = builder.substring(builder.indexOf("money=") + 6,
                            builder.length());
                    double money01= Double.parseDouble(money)/100;
                    intent.putExtra("money", money01+"");
                    intent.putExtra("itemid", itemid);
                    intent.putExtra("pay_code_type","qianmi");
                    startActivity(intent);
                    finish();
                }
                if(url.contains("com.ihomebank://back")){
                    finish();
                }
                if(url.contains("com.ihomebank://?")){
//                    com.ihomebank://?type=ESTORE_PAY&items=xxxx&money=600&uid=xxx
                    Intent intent = new Intent(WebActivity.this,
                            PayActivity.class);
                    String builder1 = builder.substring(0, builder.lastIndexOf("&"));
                    String itemid = builder1.substring(builder.indexOf("items=") + 6,
                            builder1.lastIndexOf("&"));
                    String money = builder1.substring(builder.indexOf("money=") + 6,
                            builder1.length());
                    double money01= Double.parseDouble(money)/100;
                    intent.putExtra("money", money01+"");
                    intent.putExtra("itemid", itemid);
                    intent.putExtra("pay_code_type","estore");
                    startActivity(intent);
//                    finish();
                }
                return false;
            }
        });

        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    myProgressBar.setVisibility(View.GONE);
                } else {
                    if (View.GONE == myProgressBar.getVisibility()) {
                        myProgressBar.setVisibility(View.VISIBLE);
                    }
                    myProgressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

        });

    }

    @Override
    protected void onReadIntent(Bundle bundle) {
        // TODO Auto-generated method stub

    }
}
